package com.icode.gen;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import com.icode.utils.DBConnection;

public class AutoGen {

	/**
	 * = * @param args
	 */
	public static void main(String[] args) {
		AutoGen ag = new AutoGen();
		ag.DBMeta();
	}

	public void DBMeta() {
		Connection conn = DBConnection.getConnection();
		PreparedStatement st = null;
		DatabaseMetaData meta;
		try {
			meta = conn.getMetaData();
			ResultSet res = meta.getTables(null, null, null,
					new String[] { "TABLE" });
			while (res.next()) {
				String tableName = res.getString("TABLE_NAME");
				st = conn.prepareStatement("SELECT * FROM " + tableName);
				ResultSet rs = st.executeQuery();
				ResultSetMetaData rsMeta = rs.getMetaData();
				this.genTable(tableName, rsMeta);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void genTable(String tableName, ResultSetMetaData rsMeta)
			throws SQLException, FileNotFoundException {
		int columnCount = rsMeta.getColumnCount();
		StringBuffer strBuff = new StringBuffer();
		String br = "\n\r";
		// Class name start with uppercase
		tableName = Character.toUpperCase(tableName.charAt(0))
				+ tableName.substring(1);
		String strVar = "";
		String strGS = "";
		String strCon = "";
		String strImport = "";

		for (int i = 1; i <= columnCount; i++) {
			// Get column name and column type
			String colName = rsMeta.getColumnName(i);
			String colType = rsMeta.getColumnTypeName(i);
			// Convert sql type to java type
			if (colType.equals("varchar") || colType.equals("nvarchar")
					|| colType.equals("nchar") || colType.equals("char")
					|| colType.equals("varbinary")) {
				colType = "String";
			}
			// Create variables
			String varName = colName;
			strVar += "private " + colType + " " + varName + ";\n";

			// After "get" and "set" in method name start with uppercase
			colName = Character.toUpperCase(colName.charAt(0))
					+ colName.substring(1);
			// Create getter and setter
			strGS += "public " + colType + " get" + colName
					+ "(){\nreturn this." + varName + ";\n}" + br;

			strGS += "public void set" + colName + "(" + colType + " "
					+ varName + "){\n" + "this." + varName + " = " + varName
					+ ";\n}" + br;
		}
		// Create constructor
		strCon += "public " + tableName + "(){" + br + "}" + br;

		// Comlete string
		strBuff.append("package com.icode.entities;" + br);
		strBuff.append("public class " + tableName + " {" + br);
		strBuff.append(strVar + br);
		strBuff.append(strCon);
		strBuff.append(strGS);
		strBuff.append("}");

		// Write this string to file
		File f = new File("E:/Java/JAVA/Tourist_ICODE/src/com/icode/entities/"
				+ tableName + ".java");
		PrintWriter out = new PrintWriter(f);
		out.write(strBuff.toString());
		out.flush();
		out.close();
	}
}
